home *** CD-ROM | disk | FTP | other *** search
/ Utilities Professional 1-1500 / Utilities Professional 1-1500 (1994)(WPD)[!].iso / 12511500 / var1453.dms / var1453.adf / Messages / Example3.c < prev    next >
C/C++ Source or Header  |  1992-05-02  |  5KB  |  195 lines

  1. /***********************************************************/
  2. /*                                                         */
  3. /* Amiga C Encyclopedia (ACE) V3.0      Amiga C Club (ACC) */
  4. /* -------------------------------      ------------------ */
  5. /*                                                         */
  6. /* Book:    ACM System                  Amiga C Club       */
  7. /* Chapter: Messages                    Tulevagen 22       */
  8. /* File:    Example3.c                  181 41  LIDINGO    */
  9. /* Author:  Anders Bjerin               SWEDEN             */
  10. /* Date:    92-05-02                                       */
  11. /* Version: 1.00                                           */
  12. /*                                                         */
  13. /*   Copyright 1992, Anders Bjerin - Amiga C Club (ACC)    */
  14. /*                                                         */
  15. /* Registered members may use this program freely in their */
  16. /*     own commercial/noncommercial programs/articles.     */
  17. /*                                                         */
  18. /***********************************************************/
  19.  
  20. /* This program will open two ports. The first port is */
  21. /* used by the Timer Device while the second port will */
  22. /* not be used at all (it is a dummy port). The reason */
  23. /* why this example opens an extra port is because I   */
  24. /* want to demonstrate how you can handle two message  */
  25. /* ports simultaniously. With this technique you can   */
  26. /* monitor up to 32 ports (the system usually needs    */
  27. /* some singnals, but you have at least 16 signals for */
  28. /* yourself).                                          */
  29.  
  30.  
  31.  
  32. #include <exec/types.h>    /* STRPTR         */
  33. #include <exec/ports.h>    /* struct Message */
  34. #include <exec/memory.h>   /* MEMF_PUBLIC    */
  35. #include <exec/nodes.h>    /* NT_MESSAGE     */
  36. #include <devices/timer.h> /* TIMERNAME */
  37.  
  38.  
  39.  
  40. /* Declare two pointers to our message ports: */
  41. struct MsgPort *timerport, *dummyport;
  42. struct Message *msg;
  43.  
  44.  
  45.  
  46. /* Timer Device: */
  47. struct timerequest *tr;
  48. BOOL error = TRUE;
  49.  
  50.  
  51.  
  52. /* Declare the functions: */
  53. void clean_up();
  54. void main();
  55.  
  56.  
  57.  
  58. void main()
  59. {
  60.   /* We will store the port's signal bit values in these */
  61.   /* variables: (Up to 32 bits, which is enough)         */
  62.   ULONG dummy_sig;
  63.   ULONG timer_sig;
  64.   ULONG received_sig;
  65.  
  66.  
  67.  
  68.   /* The dummy port: */
  69.   /* Create a message port: (No name, priority 0) */
  70.   dummyport = (struct MsgPort *) CreatePort( NULL, 0 );
  71.  
  72.   /* Check if we have successfully created the port: */ 
  73.   if( !dummyport )
  74.     clean_up( "Could not create the dummy port!" );
  75.  
  76.  
  77.  
  78.   /* The Timer Device's port:                       */  
  79.   /* Create a message port through which the system */
  80.   /* will be able to communicate with us:           */
  81.   timerport = (struct MsgPort *)
  82.     CreatePort( NULL, 0 );
  83.  
  84.   /* Check if we have successfully created a port: */ 
  85.   if( !timerport )
  86.     clean_up( "Could not create the timer port!" );
  87.  
  88.  
  89.  
  90.   /* Try to alloacte and initialize a timerequest      */
  91.   /* structure, to which we will send our requests to: */
  92.   tr = (struct timerequest *)
  93.     CreateExtIO( timerport, sizeof( struct timerequest) );
  94.   /* Have we successfully created the timerequest structure? */
  95.   if( !tr )
  96.     clean_up( "Could not create the IO!" );
  97.  
  98.  
  99.  
  100.   /* Open the Timer Device: */
  101.   error = OpenDevice( TIMERNAME, UNIT_VBLANK, tr ,0 );
  102.   /* Have we successfully opened the device? */
  103.   if( error )
  104.     clean_up( "Could not open the Timer Device!" );
  105.  
  106.  
  107.  
  108.   /* Set our request: (Wait 10 seconds) */
  109.   tr->tr_node.io_Command = TR_ADDREQUEST;
  110.   tr->tr_time.tv_secs = 10;
  111.   tr->tr_time.tv_micro = 0;
  112.  
  113.  
  114.  
  115.   /* Do our request and return immediately: */
  116.   SendIO( tr );
  117.  
  118.  
  119.  
  120.   /* Get the ports signal values and transform them */
  121.   /* into bit field signals:                        */
  122.   dummy_sig = 1 << dummyport->mp_SigBit;
  123.   timer_sig = 1 << timerport->mp_SigBit;
  124.  
  125.  
  126.  
  127.   /* Wait until we receive a message from either */
  128.   /* the timer or dummy message port:            */
  129.   received_sig = Wait( dummy_sig | timer_sig );
  130.  
  131.  
  132.  
  133.   /* Check which message port sent the signal: */
  134.   /* Was it the dummy port? */
  135.   if( received_sig & dummy_sig )
  136.   {
  137.     printf( "The dummy message port! (Que?)\n" );
  138.  
  139.     /* Remove all messages: */
  140.     while( msg = (struct Message *) GetMsg( dummyport ) )
  141.       /* Do nothing... */;
  142.   }
  143.  
  144.  
  145.  
  146.   /* Was it the timer port? */
  147.   if( received_sig & timer_sig )
  148.   {
  149.     printf( "The timer message port!\n" );
  150.  
  151.     /* Remove all messages: */
  152.     while( msg = (struct Message *) GetMsg( timerport ) )
  153.       /* Do nothing... */;
  154.   }
  155.  
  156.  
  157.  
  158.   /* Clean up and leave: */  
  159.   clean_up( "The End!" );
  160. }
  161.  
  162.  
  163.  
  164. void clean_up( text )
  165. STRPTR text;
  166. {
  167.   /* Close the Timer Device: */
  168.   if( !error )
  169.     CloseDevice( tr );
  170.  
  171.  
  172.   /* Delete the timerequest structure: */
  173.   if( tr )
  174.     DeleteExtIO( tr, sizeof( struct timerequest) );
  175.  
  176.  
  177.   /* Close the timer's message port: */ 
  178.   if( timerport )
  179.     DeletePort( timerport);
  180.  
  181.  
  182.   /* Close the dummy message port: */
  183.   if( dummyport )
  184.     DeletePort( dummyport );
  185.  
  186.  
  187.   /* Print the message: */
  188.   printf( "%s\n", text );
  189.  
  190.  
  191.   /* Quit: */
  192.   exit( 0 );
  193. }
  194.  
  195.